package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author げっしょく
 * @date 2021/10/27
 */
@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    /**
     * 对指定动态发布评论
     *
     * @param map
     * @return
     */
    @PostMapping
    public ResponseEntity saveComments(@RequestBody Map map) {
        String movementId = (String) map.get("movementId");
        String comment = (String) map.get("comment");
        commentsService.saveComments(movementId, comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询动态的评论
     *
     * @param page
     * @param pagesize
     * @param movementId 动态id
     * @return
     */
    @GetMapping
    public ResponseEntity getComments(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize,
                                      String movementId) {
        PageResult pageResult = commentsService.getComments(page, pagesize, movementId);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable String id) {
        Integer integer = commentsService.commentlike(id ,true);
        return ResponseEntity.ok(integer);
    }

    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable String id) {
        Integer integer = commentsService.commentlike(id, false);
        return ResponseEntity.ok(integer);
    }
}
